const http=require('http')
const ejs=require('ejs')
const _url=require('url')
const server=http.createServer((req,res)=>{
    let route=_url.parse(req.url).pathname
    console.log(route)
    res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'})

    if(route=='/dome'){
        console.log(route)
        ejs.renderFile('./view/dome.ejs',(err,data)=>{
            if(err){
                console.log(err.message)
                return
            }
            res.end(data)
        })
    }else{
        // 渲染模板
        ejs.renderFile('./view/index.ejs',{user:['mary','bob','simba'],tokens:false},(err,str)=>{
            if(err){
                console.log(err.message)
                return
            }
            res.end(str)
        })
    }
    
})
server.listen(8080,'127.1.0.1',()=>{
    console.log(`服务器运行在:http://127.1.0.1:8080`)
})